package service.system;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.hibernate.Query;
import org.hibernate.Session;

import common.StringHelper;
import domain.entity.SysMenu;
import service.common.HibernateSessionFactory;

public class MenuService {
	public static List<SysMenu> getChildMenuByParent(String parentid, Map<String, String> hasAuthrity)
    {
        List<SysMenu> list = new ArrayList<SysMenu>();
       
            try
            {
            	Session session = HibernateSessionFactory.currentSession();
                if (StringHelper.isNullOrWhiteSpace(parentid)){
         			Query query = session.createQuery("from SysMenu where parentID is null and enable=1 order by Sort asc");
         			return query.list();
         			
                }
                   
                int cid = Integer.parseInt(parentid);
                Query query = session.createQuery("from SysMenu where ParentID=? and Enable=1 order by Sort asc");
                query.setInteger(0,cid);  
                List<SysMenu> temp =query.list();
                HibernateSessionFactory.closeSession();
                for (SysMenu menu : temp)
                {
                    if (hasAuthrity.containsKey(menu.getUrl() + "_POST") ||
                        hasAuthrity.containsKey(menu.getMenuId().toString()))
                    {
                    	SysMenu m = menu;
                        list.add(m);
                    }
                }
                return list;
            }
            catch (Exception ex)
            {
                return new ArrayList<SysMenu>();
            }
        
    }
    public static List<SysMenu> GetThreeChildMenuByParent(Integer parentid)
    {
    	Session session = HibernateSessionFactory.currentSession();
            try
            {
                if (parentid!=null)
                {
                	 Query query = session.createQuery("from SysMenu where ParentID=? and Enable=1 order by Sort asc");
                     query.setInteger(0,parentid);  
                     List<SysMenu> temp =query.list();
                     HibernateSessionFactory.closeSession();
                    return temp;
                }

                return new ArrayList<SysMenu>();
            }
            catch (Exception ex)
            {
                return new ArrayList<SysMenu>();
            }
        
    }
}
